/*==================================================
Project:       Targeting Social Programs
Authors:       Diether W. Beuermann
               Bridget Hoffmann        
               Marco Stampini 
               David L. Vargas
               Diego Vera-Cossio
----------------------------------------------------
Creation Date:    
Modification Date:   
Do-file version:    01
References:          
Output:             
==================================================*/

/*==================================================
        0: Program set up
==================================================*/
drop _all
set varabbrev off	// no variable abbreviations allowed (personal preference)

** source dir
cd "${dir5r}" // graphs dir

/*==================================================
        1: load and transformations
==================================================*/
*------------ poverty line
scalar epovline = 137350
scalar l_epovline = log(epovline)

*---------- 1.1: Load data 
use "${dir3r}/01_survey/survey_targeting_r1_long.dta", replace

*---------- 1.2: Relevant macros
glo losses job_loss ilness death divorce bankrupt theft conflict cut_remittance fire nat_disaster


*---------- 1.3: build dataset

// any shock
egen any_shock = rowmax($losses)
egen any_shock_nl = rowmax(ilness death divorce bankrupt theft conflict cut_remittance fire nat_disaster)


g _aux =(l_pp_inc_srv-l_epovline)*100 if year==2019
bys id_vivienda_SISBEN : egen dtile_l_pp_inc_srv2019 = mean(_aux)


#delimit ;
binscatter  job_loss any_shock_nl dtile_l_pp_inc_srv2019 [aw=pondera] if year==2019,  nquantiles(5) line(none)  xline(1, lcolor(black))   legend(order(1 "Involuntary job loss" 2 "Non-labor market shock")
        pos(6) col(2))
        ytitle("Prob. experiencing a shock in 2019")
        xtitle("Distance to extreme poverty line in 2019 (%)")
;
#delimit cr	

graph export "F1_pr_shock_inca.png", as(png) replace
graph export "F1_pr_shock_inca.pdf", as(pdf) replace

#delimit ;
binscatter  job_loss any_shock_nl dtile_l_pp_inc_srv2019 [aw=pondera] if year==2020,  nquantiles(5) line(none)  xline(1, lcolor(black))   legend(order(1 "Involuntary job loss" 2 "Non-labor market shock")
        pos(6) col(2))
        ytitle("Prob. experiencing a shock in 2020")
        xtitle("Distance to extreme poverty line in 2019 (%)")
;
#delimit cr	

graph export "F1_pr_shock_incb.png", as(png) replace
graph export "F1_pr_shock_incb.pdf", as(pdf) replace



